﻿using log4net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace WindowsService1
{
    internal class SocketServer
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(SocketServer));

        TcpClient tcpClient;
        public SocketServer() { 
            tcpClient = new TcpClient();
            tcpClient.Connect("localhost",8081);

            receive();
        }

        private void receive()
        {
            if (!tcpClient.Connected)
            {
                log.Error("socket连接失败");
                tcpClient.Close();
                return;
            }

            log.Info("socket服务启动成功");

            NetworkStream strean=tcpClient.GetStream();
            byte[] buff=new byte[1024];

            while (true)
            {
                int bytesNum=strean.Read(buff,0,buff.Length);
                string receivedData = Encoding.ASCII.GetString(buff, 0, bytesNum);
                log.Info("socket服务端收到消息："+receivedData);

                Thread.Sleep(100);
            }
        }
    }


}
